package com.example.zhilongzhang.constructionpayapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;
import com.example.zhilongzhang.constructionpayapp.base.ToastManager;
import com.example.zhilongzhang.constructionpayapp.bean.GlobleBean;
import com.example.zhilongzhang.constructionpayapp.bean.LoginBean;
import com.example.zhilongzhang.constructionpayapp.bean.LoginResponseBean;
import com.example.zhilongzhang.constructionpayapp.http.HttpMethod;
import com.example.zhilongzhang.constructionpayapp.util.JsonUtil;
import com.example.zhilongzhang.constructionpayapp.util.SpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import org.apache.http.params.HttpParams;

import java.io.IOException;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SetupActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String userName = SpUtil.getUserName();
                String password = SpUtil.getPassword();

                if (!TextUtils.isEmpty(userName)
                        && !TextUtils.isEmpty(password)) {
                    login(userName, password);
                } else {
                    forwardLogin();
                }
            }
        }, 2000);
    }

    private void login(final String username, final String password) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .add("os",  "android")
                .build();
        Request request = new okhttp3.Request.Builder()
                .url(HttpMethod.BaseUrl2+"login")
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Intent intent = new Intent(SetupActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseStr=response.body().string();
                LoginBean bean = new Gson().fromJson(responseStr, LoginBean.class);
                if (bean.getCode()==0){
                    SpUtil.setUserName(username);
                    SpUtil.setPassword(password);
                    SpUtil.setToken(bean.getData().getAccess_token());
                    startActivity(new Intent(SetupActivity.this,MainActivity.class));
                    finish();
                }else {
                    Intent intent = new Intent(SetupActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();

                }


            }
        });
    }

    private void forwardLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
